package m2_work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.util.Scanner;

/**
 * @author wzh
 * @date 2020-08-04 15:41
 */
public class Producer {
    public static void main(String[] args) throws Exception {

        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.249.129");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");


        Channel c = f.newConnection().createChannel();

        //定义队列

        c.queueDeclare("helloword", false, false, false, null);


        //发消息
        while (true) {
            //控制台输入的消息发送到rabbitmq
            System.out.print("输入消息: ");
            String msg = new Scanner(System.in).nextLine();
            //如果输入的是"exit"则结束生产者进程
            //参数:exchage,routingKey,props,body
            if ("exit".equals(msg)) {
                break;
            }
            c.basicPublish("", "helloworld", MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
            System.out.println("消息已发送: " + msg);
        }

        c.close();

    }
}
